//
//  HomeTableViewController.swift
//  XMGWB
//
//  Created by xiaomage on 15/12/1.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

import UIKit
import AFNetworking

class HomeTableViewController: BaseTableViewController {
    
    var statuses:[Statuses]?{
        didSet{
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin{
            visitorLoginView?.setupVistorInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention",target: self, action: #selector(HomeTableViewController.leftBtnDidClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightBtnDidClick))
        //let titleBtn: TitleButton = TitleButton()
        
        titleBtn.setTitle("海源", forState: .Normal)
        titleBtn.addTarget(self, action: #selector(HomeTableViewController.navBtnDidClick(_:)), forControlEvents: .TouchUpInside)
        navigationItem.titleView = titleBtn
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.PopoverShouldPresent), name: HYPresentationControllerManegerDidPresented, object: presentationManager)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.PopoverShouldDismiss), name: HYPresentationControllerManegerDidDismiss, object: presentationManager)
        loadUserData()
        
    }
    private func loadUserData(){
        assert(UserAccount.loadUserAccount() != nil, "必须授权之后才能获取微博数据")
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameter = ["access_token": UserAccount.loadUserAccount()!.access_token!]
        
        let AFN = AFHTTPSessionManager()
        
        AFN.GET(urlString, parameters: parameter, success: { (_, result) in
            guard let arr = (result as! [String: AnyObject])["statuses"] as? [[String: AnyObject]] else { return }
            //HYLog(arr)
            var models = [Statuses]()
            for dict in arr{
                let status = Statuses(dict: dict)
                models.append(status)
            }
            //HYLog(models)
            self.statuses = models
            HYLog(self.statuses)
            
            }) { (_, error) in
                HYLog(error)
        }
        
        
    }
    @objc private func navBtnDidClick(btn: TitleButton){
        btn.selected = !btn.selected
        //HYLog("")
        let sb = UIStoryboard(name: "Popover", bundle: nil)
        guard let vc = sb.instantiateInitialViewController() else{return}
        //自定义专场动画
        vc.transitioningDelegate = presentationManager
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(vc, animated: true) { 
            
        }
    }
    @objc private func PopoverShouldPresent(){
        titleBtn.selected = true
    }
    @objc private func PopoverShouldDismiss(){
        titleBtn.selected = false
    }
    @objc private func leftBtnDidClick(){
        //HYLog(#function)
        
    }
    @objc private func rightBtnDidClick(){
        //HYLog(#function)
        let sb = UIStoryboard(name: "QRCode", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        presentViewController(vc!, animated: true) { 
            
        }
    }
    
    private lazy var presentationManager: HYPresentationControllerManeger = {
        let manager = HYPresentationControllerManeger()
        manager.presentViewFrame = CGRect(x: 90, y: 50, width: 200, height: 200)
        return manager
    }()
    private lazy var titleBtn: TitleButton = {
        let btn = TitleButton()
        return btn
    }()
    

}
extension HomeTableViewController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("home") as! HomeTableViewCell
        cell.status = statuses![indexPath.row]
        return cell
    }
}


